var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var app = getApp();

Page(common.extendPage(PageBase, {
data: {
  orderId: 0,
  orderDtl: {},
  services: {},
  loading: true,
  storetype: '',
  distance: 0
},
/**
 * 页面初始化
 */
onLoad(options) {
  this._options = options;
  this.setData({
    orderId: this._options.orderId
  })
  this.getOrderDtl();
},
/**
 * 商家接受或拒绝服务
 */
_orderHandler(evt) {
  var paramsUrl = '/public/api/orders/acceptOrder';
  var {
    orderId,
    accept
  } = evt.currentTarget.dataset;
  var paramsData = {
    uid: wx.getStorageSync('uid'),
    oid: orderId,
    is_accept: accept
  };
  ajax.P({
    url: paramsUrl,
    data: paramsData,
    success: (res, isLoadcb) => {
      if (accept == 1){
        util.tips('商家已接受');
      }else{
        util.tips('商家已拒绝');
      }
      
      isLoadcb && isLoadcb();
    }
  })
},
/**
 * 获取订单详情
 */
getOrderDtl() {
  var servicesArr = this.data.servicesArr;
  var paramsUrl = '/public/api/orders/morderdetails';
  var paramsData = {
    id: this.data.orderId,
    uid: wx.getStorageSync('uid')
  }
  ajax.P({
    url: paramsUrl,
    data: paramsData,
    success: (res, isLoadcb) => {
      var orderDtl = res.data;
      var services = orderDtl.services ? orderDtl.services : '';
      if (services.psfee) {
        services.psfee = parseFloat(services.psfee).toFixed(2);
      }
      orderDtl.amount = parseFloat(orderDtl.amount).toFixed(2);
      services.item.forEach((item, index) => {
        var servicesArr = [];
        if (item.services && item.services.length > 0) {
          item.services.forEach((val, idx) => {
            servicesArr.push(val.split(','));
          })
          item.servicesArr = servicesArr;
          item.subtotalPrice = 0;
          item.servicesArr.forEach((o, i) => {
            item.subtotalPrice += o[2] * o[3];
            o.push(parseFloat(o[2] * o[3]).toFixed(2))
          })
          if (orderDtl.storetype == 1 || orderDtl.storetype == 2 || orderDtl.storetype == 8 || orderDtl.storetype == 9) {

            if (!!item.amountlist) {
              if (item.amountlist.ways) {
                item.subtotalPrice += parseFloat(item.amountlist.ways) + parseFloat(item.amountlist.specifications)
              } else {
                item.subtotalPrice += parseFloat(item.amountlist.specifications)
              }
            }
          }
          if (orderDtl.storetype == 3) {
            if (!!item.amountlist) {
              this._distance = item.amountlist.distance[0] || item.amountlist.distance;
              this._jiashou = item.amountlist.jiashou;
              this._handling = item.amountlist.handling;
              item.subtotalPrice += (parseFloat(item.amountlist.distance[0]) || parseFloat(item.amountlist.distance)) + parseFloat(item.amountlist.handling) + parseFloat(item.amountlist.jiashou);
            }
          }
        }
        item.subtotalPrice = parseFloat(item.subtotalPrice).toFixed(2);
      })
      this.setData({
        orderDtl,
        services,
        storetype: orderDtl.storetype,
        loading: false,
        handling: parseFloat(this._handling).toFixed(2),
        distance: parseFloat(this._distance).toFixed(2),
        jiashou: parseFloat(this._jiashou).toFixed(2)
      })
      isLoadcb && isLoadcb();
    }
  })
}
}));